/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useRef, useState } from 'react';
import DragTable from '@/components/DragTable';
import type { Columns } from '@/components/_pages/tools/refundRequest/_utils/type';
import {
  getColumns,
  ModalFormColFormat,
} from '@/components/_pages/tools/refundRequest/_utils/index.config';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import { getRefundReviewList } from '@/services/tools/refundRequest';
import { usePaginationCache } from '@base/components/keep-alive/_utils';
import KbRefundModal from '../refund-modal';
import ProTable from '@ant-design/pro-table';
import { Space } from 'antd';

interface KbRefundReviewProps {
  show: boolean;
  status: string;
}
const KbRefundReview = (props: KbRefundReviewProps) => {
  const { show, status } = props;
  const [modalType, updateType] = useState<string>('');
  const [visible, setVisible] = useState<boolean>(false);
  const [record, setRecord] = useState<Columns | any>({});
  const actionRef = useRef<ActionType>();

  const handleOpen = (record_: Columns, type: string) => {
    updateType(type);
    setRecord(record_);
    setVisible(true);
  };
  const handleClose = () => {
    setRecord({});
    setVisible(false);
  };

  const columns: ProColumns[] = [
    ...getColumns({ status }),
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align: 'center',
      width: 100,
      render: (_: any, record_: Columns) => {
        return record_.status == 0 ? (
          <Space>
            <a onClick={handleOpen.bind(null, record_, 'pass')}>退款</a>
            <a onClick={handleOpen.bind(null, record_, 'refuse')}>拒绝</a>
          </Space>
        ) : null;
      },
    },
  ];

  const pagination = usePaginationCache('table', {
    pageSize: 20,
    showQuickJumper: false,
    showSizeChanger: false,
  });

  useEffect(() => {
    if (show) {
      actionRef.current?.reload();
    }
  }, [show]);
  return (
    <DragTable>
      <ProTable<ProColumns<Columns[]>>
        rowKey='id'
        toolBarRender={false}
        pagination={pagination}
        request={getRefundReviewList}
        params={{ status }}
        actionRef={actionRef}
        columns={columns}
        scroll={{ x: 1200 }}
      />
      <KbRefundModal
        layout='horizontal'
        actionRef={actionRef}
        visible={visible}
        modalProps={{
          onCancel: handleClose,
        }}
        callback={() => {
          handleClose();
          actionRef.current?.reload();
        }}
        record={record}
        modalType={modalType}
        {...ModalFormColFormat}
      />
    </DragTable>
  );
};

export default KbRefundReview;
